package com.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.io.FileSystemResource;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.DefaultTemplateResolver;
import org.thymeleaf.templateresolver.ITemplateResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
import org.thymeleaf.templateresolver.TemplateResolution;

import java.io.IOException;

/**
 * @classDescription:
 * @author:xiayingjie
 * @createTime:16/7/15
 */
//@Configuration
@EnableWebMvc
@EnableAspectJAutoProxy
@ComponentScan({"com.**.aop","com.**.controller"})
public class WebConfig extends WebMvcConfigurerAdapter{






    //视图解析器 未配置默认使用BeanNameViewResolver
    @Bean
    public ViewResolver viewResolver(){
        InternalResourceViewResolver  irvr=new InternalResourceViewResolver();
        irvr.setPrefix("/WEB-INF/views/");
        irvr.setSuffix(".jsp");
        irvr.setExposeContextBeansAsAttributes(true);
        return irvr;
    }

    //上传 依赖servlet3.0对 multipart请求支持，始于spring3.1 需要委托给dispatchservlet
    //不是必须 当上传参数是@RequestPart("file") MultipartFile file 必须
    @Bean
    public MultipartResolver multipartResolver() throws IOException{
        //CommonsMultipartResolver 使用FileUpload解析multipart
//        CommonsMultipartResolver resolver=new CommonsMultipartResolver();
//        resolver.setUploadTempDir(new FileSystemResource("/"));
//        resolver.setMaxUploadSize(2*1024*1024);
//        resolver.setMaxInMemorySize(0);
        return new StandardServletMultipartResolver();
    }
    //配置静态资源处理器，如果是静态资源，则自动跳过
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        //将静态资源调用转到servlet默认容器处理，dispatcherServlet不处理
        configurer.enable();
    }






}
